<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>巡检项管理</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link href="/static/plugins/bootstrap-table/bootstrap-table.css" rel="stylesheet">
    <link href="/static/css/style.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/css/public.css">
    <link rel="stylesheet" href="/static/css/font-awesome.min.css">
    <!-- jquery 提示插件css -->
    <link href="/static/plugins/bootstrap-tree/bootstrap-treeview.css" rel="stylesheet">

    <link href="/static/plugins/toastr.min.css" rel="stylesheet">
    <style>
        .manage-title{
            width: 74px;
        }
        .form-manage {
            width: 71%;
        }
    </style>
</head>
<body style="background-color:#eee">
<div id="box" style="margin-top: 10px">
    <div class="float-clear">
        <div class="float-left contentBox border-all" style="width:18%;margin: 0 0.5%;">
            <div class="content-title border-bottom">
                <span>巡检项管理</span>
            </div>
            <div class="content-center">
                <p class="text-center" v-if="leftTreeLoading">正在加载中,请稍后.....</p>
                <p class="text-center" v-if="!leftTreeLoading && leftTreeList.length==0">暂时没有数据,请添加</p>
                <div id="treeview7" class="test"></div>
            </div>
        </div>
        <div class="float-left contentBox border-all" style="width:80.5%;overflow: auto;">
            <div class="content-title border-bottom" style="position:relative;">
                <span>{{treeTitle.name}}</span>
            </div>
            <div class="ibox-content" style="position:relative;">
                <button class="btn btn-default pull-right my-btn my-btn-remove" onclick="deleteClick()">删除</button>
                <button class="btn btn-default pull-right my-btn my-btn-pencil" onclick="editClick()">编辑</button>
                <button class="btn btn-default pull-right my-btn my-btn-add" onclick="addBtnClick()">添加</button>
                <button class="btn btn-default my-btn my-search-btn my-btn-search" style="top: 15px;left: 220px;">查询
                </button>
                <table id="table"
                       data-toggle="table"
                       data-toolbar=".toolbar"
                       data-pagination="true"
                       data-maintain-selected="true"
                       data-search="true"
                       data-show-refresh="true"
                       data-search-align="left"
                       data-toolbar-align="right">
                    <thead>
                    <tr>
                        <th data-width="40"></th>
                        <th data-width="400" data-field="content">巡检项内容</th>
                        <th data-width="400" data-field="device_type_id" data-formatter="checkBoxDeviceType">设备类型</th>
                        <th data-width="300" data-field="code">巡检项编码</th>
                        <th data-width="100" data-field="status" data-align="center" data-formatter="checkBoxFormatter">有效
                        </th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
    <!--添加模块-->
    <div id="inspectionModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">{{modalType}}管理-{{treeTitle.name}}</h4>
                </div>
                <div class="modal-manage">
                    <div class="form-group col-xs-6">
                        <div class="manage-title">
                            <span>巡检项内容<span class="glyphicon glyphicon-asterisk mandatory"></span></span>
                        </div>
                        <input type="text" class="form-control form-manage" placeholder="" v-model="modalData.content">
                    </div>
                    <div class="form-group col-xs-6">
                        <div class="manage-title">
                            <span>巡检项编码<span class="glyphicon glyphicon-asterisk mandatory"></span></span>
                        </div>
                        <input type="text" class="form-control form-manage" placeholder="" v-model="modalData.code">
                    </div>
                    <div class="form-group col-xs-6">
                        <div class="manage-title">
                            <span>设备类型<span class="glyphicon glyphicon-asterisk mandatory"></span></span>
                        </div>
                        <select class="form-control form-manage" style="padding: 0 8px;" v-model="deviceType">
                            <option v-for="item in deviceTypeList" :value="item.name">{{item.name}}</option>
                        </select>
                    </div>
                    <div class="form-group col-xs-6">
                        <div class="manage-title">
                            <span>选项：</span>
                        </div>
                        <div class="pull-left" style="margin-top: 3px;">
                            <label style="margin-right: 5px;cursor: pointer;">
                                <input id="F_IsMenu" type="checkbox" v-model="modalData.status" @click="changeCheck(modalData.status)">
                                启用
                            </label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary submit" onclick="add()">确定</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    <!--删除模态框-->
    <div id="deleteModal" class="modal fade">
        <div class="modal-dialog" style="width: 400px;">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" style="float: left;">温馨提示</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                </div>
                <div class="modal-manage">
                    <img src="/static/image/delete.png" alt="" style="float: left;margin-left: 20px">
                    <h3 class="text-center" style="float: left;margin-top: 57px;margin-left: 40px">是否删除当前项</h3>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary submit" onclick="deleteTable()">确认</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
</div>
<!-- 全局js -->
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/plugins/bootstrap-table/bootstrap-table.js"></script>
<script src="/static/plugins/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
<script src="/static/js/jquery.sparkline.min.js"></script>
<script type="text/javascript" src="/static/js/echarts.min.js"></script>
<script src="/static/js/vue.min.js"></script>
<!--bootstrap-ree-->
<script src="/static/plugins/bootstrap-tree/bootstrap-treeview.js"></script>
<script src="/static/js/publicServe.js"></script>
<script src="/static/plugins/toastr/toastr.min.js"></script>
<script>
    var vm = new Vue({
        el: '#box',
        data: {
            leftTreeLoading: true,               //左侧树结构加载判断
            leftTreeList:[],           //左边ztree数据
            treeTitle: {
                id: null,
                name: '巡检项管理'
            },                  //模态框title的类型 添加|编辑
            tableList:[],               //table信息
            modalType:'添加',                //弹框  添加||删除
            modalData:{
                code:'',                    //巡检项编码
                status:null,                    //启用禁用
                content:'',                     //巡检项内容
                device_type_id:null,              //设备类型id
            },
            deviceTypeList:[],                  //设备类型
            deviceType: null,   //模态选择框中选定的设备类型id
        },
        created: function () {

        },
        methods: {
            //选择模态 菜单 状态
            changeCheck: function (item) {
                item = !item;
            }
        }
    });
    // 数据列表
    var $table = $('#table');
    //为bootstrapTable添加每一行的事件监听
    $table.on('click-row.bs.table', function (r, row, element) {
        $('.table-line-active').removeClass('table-line-active');
        $(element).addClass('table-line-active');
        //点击以后将当前行的数据赋值给vm.modalData
        vm.modalData = copyFun(row);
    });
    $table.bootstrapTable({
        columns:                 //列表序号
            [{
                //field: 'Number',//可不加
                // title: 'Number',//标题  可不加
                formatter: function (value, row, index) {
                    return index + 1;
                }
            }],
    });
    //获取设备类型列表
    function getDeviceTypeList() {
        $.ajax({
            url: '/core/device_type/get_all/',
            type: 'POST',
            dataType: "json",
            data: '',
            async: true,
            contentType: "application/json",
            success: function (response) {
                if (response && response.success == 1) {
                    vm.deviceTypeList = response.data;
                } else {
                    toastr.info(response.error_msg);
                }
            }
        });
    }
    //获取zTreee
    function getTreeList() {
        vm.leftTreeLoading = true;
        $.ajax({
            url: '/core/device_type/get_all/',
            type: 'POST',
            dataType: "json",
            data: '',
            async: true,
            contentType: "application/json",
            success: function (response) {
                if (response && response.success == 1) {
                    vm.leftTreeLoading = false;
                    vm.leftTreeList = response.data;
                    //绘制zTree树
                    renderZTree(vm.leftTreeList);
                    //获取数据中第一条 的下级 table list结构
                    if(vm.leftTreeList[0]){
                        vm.treeTitle.name = vm.leftTreeList[0].name;
                        vm.treeTitle.id = vm.leftTreeList[0].id;
                        getTableList(vm.leftTreeList[0].id);
                    }
                } else {
                    toastr.info(response.error_msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                switch (XMLHttpRequest.status) {
                    case(500):
                        toastr.info('服务器系统内部错误');
                        break;
                    case(403):
                        toastr.info("无权限执行此操作");
                        break;
                    case(408):
                        toastr.info("请求超时");
                        break;
                    default:
                        toastr.info(XMLHttpRequest.responseText);
                }
            }
        });
    }
    //绘制左侧树
    function renderZTree(tableList){
        $('#treeview7').treeview({
            color: "#666",
            showBorder: false,
            data: tableList,
            onNodeSelected: function (event, data) {
                vm.treeTitle = {
                    id: data.id,
                    name: data.name
                };
                getTableList(data.id);
            }
        });
    }
    //获取table数据
    function getTableList(id) {
        $.ajax({
            url: '/core/patrol_manage/get_all_by_device_type/',
            type: 'POST',
            dataType: "json",
            data: JSON.stringify({id: id}),
            async: true,
            contentType: "application/json",
            success: function (response) {
                if (response && response.success == 1) {
                    vm.tableList = response.data;
                    for (var i=0;i<vm.tableList.length;i++){
                        var status = vm.tableList[i].status==0 ? true : false;
                        vm.tableList[i].status = status;
                    }
                    //绘表格数据
                    $table.bootstrapTable('load', vm.tableList);
                } else {
                    toastr.info(response.error_msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                switch (XMLHttpRequest.status) {
                    case(500):
                        toastr.info('服务器系统内部错误');
                        break;
                    case(403):
                        toastr.info("无权限执行此操作");
                        break;
                    case(408):
                        toastr.info("请求超时");
                        break;
                    default:
                        toastr.info(XMLHttpRequest.responseText);
                }
            }
        });
    }
    //添加
    function addBtnClick(){
        if(vm.leftTreeList[0]){
            vm.deviceType=vm.treeTitle.name;
        }
        //清空 vm.modalData 中的值
        vm.modalData = {
            code:'',                    //巡检项编码
            status:null,                    //启用禁用
            content:'',                     //巡检项内容
            device_type_id:null,              //设备类型id
        };
        $('.table-line-active').removeClass('table-line-active');
        vm.modalType='添加';
        $("#inspectionModal").modal('show');
    }
    //编辑
    function editClick(){
        vm.deviceType = null;   //模态选择框中选定的设备类型
        //通过类型ID为deviceType 赋值
        for (var i = 0; i < vm.deviceTypeList.length; i++) {
            if (vm.deviceTypeList[i].id == vm.modalData.device_type_id) {
                vm.deviceType = vm.deviceTypeList[i].name;
            }
        }
        if (!vm.modalData.id) {
            toastr.info('编辑时最少需要选中一行!');
            return
        }
        vm.modalType='编辑';
        $("#inspectionModal").modal('show');
    }
    //删除
    function deleteClick(){
        if (!vm.modalData.id) {
            toastr.info('删除时最少需要选中一行!');
            return
        }
        $("#deleteModal").modal('show');
    }
    //模态框  添加||编辑  确定
    function add(){
        //通过类型ID为deviceType 赋值
        for (var i = 0; i < vm.deviceTypeList.length; i++) {
            if (vm.deviceTypeList[i].name == vm.deviceType) {
                vm.modalData.device_type_id = vm.deviceTypeList[i].id;
            }
        }
        //深拷贝
        var newModalData = copyFun(vm.modalData);
        vm.modalData.status ? newModalData.status = 0 : newModalData.status = 1;      //启用||禁用
        if(vm.modalType=='添加'){
            if (!vm.modalData.code) {
                toastr.warning('巡检项编码不能为空!');
                return
            }
            if (!vm.modalData.device_type_id) {
                toastr.info('请选择设备类型!');
                return
            }
            if (!vm.modalData.content) {
                toastr.warning('巡检项内容不能为空!');
                return
            }
            $.ajax({
                url: '/core/patrol_manage/add/',
                type: 'POST',
                dataType: "json",
                data:JSON.stringify(newModalData),
                async: true,
                contentType: "application/json",
                success: function (response) {
                    if (response && response.success==1) {
                        $("#inspectionModal").modal('hide');
                        //重新 绘制table
                        getTableList(vm.treeTitle.id);
                        toastr.success('添加成功')
                    }else{
                        toastr.info(response.error_msg)
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    switch (XMLHttpRequest.status) {
                        case(500):
                            toastr.info('服务器系统内部错误');
                            break;
                        case(403):
                            toastr.info("无权限执行此操作");
                            break;
                        case(408):
                            toastr.info("请求超时");
                            break;
                        default:
                            toastr.info(XMLHttpRequest.responseText);
                    }
                }
            });
        }else{
            $.ajax({
                url: '/core/patrol_manage/edit/',
                type: 'POST',
                dataType: "json",
                data:JSON.stringify(newModalData),
                async: true,
                contentType: "application/json",
                success: function (response) {
                    if (response && response.success==1) {
                        $("#inspectionModal").modal('hide');
                        //重新绘制table信息
                        getTableList(vm.treeTitle.id);
                        toastr.success('编辑成功')
                    }else{
                        toastr.info(response.error_msg)
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    switch (XMLHttpRequest.status) {
                        case(500):
                            toastr.info('服务器系统内部错误');
                            break;
                        case(403):
                            toastr.info("无权限执行此操作");
                            break;
                        case(408):
                            toastr.info("请求超时");
                            break;
                        default:
                            toastr.info(XMLHttpRequest.responseText);
                    }
                }
            });
        }
    }
    //模态框 删除
    function deleteTable(){
        $.ajax({
            url: '/core/patrol_manage/del/',
            type: 'POST',
            dataType: "json",
            data:JSON.stringify({id:vm.modalData.id}),
            async: true,
            contentType: "application/json",
            success: function (response) {
                if (response && response.success==1) {
                    $("#deleteModal").modal('hide');
                    toastr.success('删除成功')
                    //重新绘制table信息
                    getTableList(vm.treeTitle.id);
                }else{
                    toastr.info(response.error_msg)
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                switch (XMLHttpRequest.status) {
                    case(500):
                        toastr.info('服务器系统内部错误');
                        break;
                    case(403):
                        toastr.info("无权限执行此操作");
                        break;
                    case(408):
                        toastr.info("请求超时");
                        break;
                    default:
                        toastr.info(XMLHttpRequest.responseText);
                }
            }
        });
    }
    //为列表添加checkBox样式
    function checkBoxFormatter(value, row, index) {
        if (value) {
            return [
                '<div id="check-box" class="open1"><div id="check" class="open2"></div></div>'
            ].join('');
        } else {
            return [
                '<div id="check-box" class="close1"><div id="check" class="close2"></div></div>'
            ].join('');
        }
    }
    //为列表添加设备类型
    function checkBoxDeviceType(value, row, index) {
        for(var i=0;i<vm.deviceTypeList.length;i++){
            if(vm.deviceTypeList[i].id==value){
                return [
                    '<span>'+vm.deviceTypeList[i].name+'</span>'
                ].join('');
            }
        }
    }
    $(document).ready(function () {
        $(".contentBox").css({"height": $(document.body).height() - 20});
        $(window).resize(function () {//当浏览器大小变化时
            $(".contentBox").css({"height": $(document.body).height() - 20})
        });
        //获取zTree信息
        getTreeList();
        //获取设备类型列表
        getDeviceTypeList();
        //获取table信息 并且绘制
        $table.bootstrapTable('load', vm.tableList);
    });

    //弹出框提示配置
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "progressBar": true,
        "preventDuplicates": false,
        "positionClass": "toast-bottom-right",
        "onclick": null,
        "showDuration": "400",
        "hideDuration": "1000",
        "timeOut": "2000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    };
</script>
</body>
</html>